package com.zzxx.wechat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zzxx.wechat.common.vo.ArcteryxCommodityVo;
import com.zzxx.wechat.common.vo.ArcteryxLinkVo;
import com.zzxx.wechat.common.vo.ArcteryxTaskReq;
import com.zzxx.wechat.exception.BusinessException;
import com.zzxx.wechat.result.R;
import com.zzxx.wechat.service.ArcteryxTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Slf4j
@CrossOrigin
@Controller
@RequestMapping("/arcteryx/task")
public class ArcteryxTaskController {
    @Resource
    private ArcteryxTaskService arcteryxTaskService;

    /**
     * 用于存储每个productNo对应的锁
     */
    private final Map<String, Integer> ARCTERYX_TASK_LIST_METHOD_MAP = new ConcurrentHashMap<>();


    @PostMapping("/list")
    @ResponseBody
    public R<List<ArcteryxCommodityVo>> list(@RequestBody ArcteryxTaskReq arcteryxTaskReq) {

        String arcteryxTaskReqMd5 = DigestUtils.md5Hex(JSON.toJSONString(arcteryxTaskReq));

        if (ARCTERYX_TASK_LIST_METHOD_MAP.get(arcteryxTaskReqMd5) != null) {
            return R.error("请求处理中.....");
        }
        ARCTERYX_TASK_LIST_METHOD_MAP.put(arcteryxTaskReqMd5, 1);

        try {
            List<String> productNoList = Arrays.asList(arcteryxTaskReq.getProductNos().split(","));

          // List<ArcteryxLinkVo> arcteryxLinkVos = arcteryxTaskService.queryLinkByProductNos(productNoList);

            List<ArcteryxCommodityVo> arcteryxCommodityVos = new ArrayList<>();

            String aa  ="[{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"us\",\"spuId\":\"1231\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/us/en/shop/1231\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"ca\",\"spuId\":\"1231\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/ca/jp/shop/1231\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"it\",\"spuId\":\"1231\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/it/en/shop/1231\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"ca\",\"spuId\":\"1231\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/ca/en/shop/1231\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"us\",\"spuId\":\"1231\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/us/jp/shop/1231\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"it\",\"spuId\":\"1231\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/it/jp/shop/1231\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"it\",\"spuId\":\"3232\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/it/en/shop/3232\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"ca\",\"spuId\":\"3232\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/ca/jp/shop/3232\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"it\",\"spuId\":\"3232\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/it/jp/shop/3232\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"us\",\"spuId\":\"3232\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/us/jp/shop/3232\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"ca\",\"spuId\":\"3232\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/ca/en/shop/3232\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL不存在,You’ve strayed off trail\",\"countryCode\":\"us\",\"spuId\":\"3232\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/us/en/shop/3232\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"countryCode\":\"it\",\"arcteryxColourVoList\":[{\"colourLabel\":\"Stone Green\",\"colourInventory\":\"XS:11|S:68|M:108|L:146|XL:95|XXL:27|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Canvas\",\"colourInventory\":\"XS:14|S:12|M:17|L:32|XL:43|XXL:29|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Black\",\"colourInventory\":\"XS:60|S:0|M:122|L:329|XL:224|XXL:76|\",\"realPrice\":\"240.00\"}],\"spuId\":\"X000009141\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/it/en/shop/X000009141\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"countryCode\":\"us\",\"arcteryxColourVoList\":[{\"colourLabel\":\"Stone Green\",\"colourInventory\":\"XS:0|S:0|M:10|L:71|XL:33|XXL:16|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Black X\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:192|XXL:77|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Canvas X\",\"colourInventory\":\"XS:9|S:37|M:133|L:126|XL:43|XXL:23|\",\"realPrice\":\"240.00\"}],\"spuId\":\"X000009141\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/us/en/shop/X000009141\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"reason\":\"该URL中不存在商品\",\"countryCode\":\"ca\",\"arcteryxColourVoList\":[{\"colourLabel\":\"Stone Green\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:147|XXL:71|\",\"realPrice\":\"280.00\"},{\"colourLabel\":\"Canvas\",\"colourInventory\":\"XS:3|S:0|M:109|L:135|XL:162|XXL:57|\",\"realPrice\":\"280.00\"}],\"spuId\":\"X000009141\",\"languageCode\":\"en\",\"existLink\":\"https://arcteryx.com/ca/en/shop/X000009141\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"countryCode\":\"us\",\"arcteryxColourVoList\":[{\"colourLabel\":\"Stone Green\",\"colourInventory\":\"XS:0|S:0|M:11|L:71|XL:33|XXL:16|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Black X\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:192|XXL:77|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Canvas X\",\"colourInventory\":\"XS:9|S:37|M:133|L:126|XL:43|XXL:23|\",\"realPrice\":\"240.00\"}],\"spuId\":\"X000009141\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/us/jp/shop/X000009141\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"countryCode\":\"ca\",\"arcteryxColourVoList\":[{\"colourLabel\":\"Stone Green\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:147|XXL:71|\",\"realPrice\":\"280.00\"},{\"colourLabel\":\"Canvas\",\"colourInventory\":\"XS:3|S:0|M:109|L:135|XL:162|XXL:57|\",\"realPrice\":\"280.00\"}],\"spuId\":\"X000009141\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/ca/jp/shop/X000009141\"},{\"dateTime\":\"2025-07-07 21:48:19\",\"countryCode\":\"it\",\"arcteryxColourVoList\":[{\"colourLabel\":\"Stone Green\",\"colourInventory\":\"XS:11|S:68|M:108|L:146|XL:95|XXL:27|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Canvas\",\"colourInventory\":\"XS:14|S:12|M:17|L:32|XL:43|XXL:29|\",\"realPrice\":\"240.00\"},{\"colourLabel\":\"Black\",\"colourInventory\":\"XS:60|S:0|M:122|L:329|XL:224|XXL:76|\",\"realPrice\":\"240.00\"}],\"spuId\":\"X000009141\",\"languageCode\":\"jp\",\"existLink\":\"https://arcteryx.com/it/jp/shop/X000009141\"}]";


            List<ArcteryxLinkVo> arcteryxLinkVos = JSON.parseArray(aa,ArcteryxLinkVo.class);
            Map<String, List<ArcteryxLinkVo>> collect = arcteryxLinkVos.stream().collect(Collectors.groupingBy(ArcteryxLinkVo::getSpuId));
            for(Map.Entry<String, List<ArcteryxLinkVo> > entry: collect.entrySet()){
                ArcteryxCommodityVo arcteryxCommodityVo = new ArcteryxCommodityVo();
                arcteryxCommodityVo.setProductNo(entry.getKey());
                arcteryxCommodityVo.setArcteryxLinkVos(entry.getValue());
                arcteryxCommodityVos.add(arcteryxCommodityVo);
            }

            return R.ok(arcteryxCommodityVos);
        } catch (BusinessException e) {
            log.warn("arcteryx-任务-业务异常:{}", e.getMessage());
            return R.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("arcteryx-任务--异常：", e);
            return R.error();
        } finally {
            ARCTERYX_TASK_LIST_METHOD_MAP.remove(arcteryxTaskReqMd5);
        }

/*         String json = "{\"success\":true,\"code\":0,\"message\":\"success\",\"data\":[{\"existLink\":\"https://arcteryx.com/us/en/shop/X000009141\",\"arcteryColourVoList\":[{\"colourLabel\":\"Stone Green\",\"realPrice\":\"240\",\"colourInventory\":\"XS:0|S:0|M:11|L:71|XL:33|XXL:16|\"},{\"colourLabel\":\"Black X\",\"realPrice\":\"240\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:193|XXL:77|\"},{\"colourLabel\":\"Canvas X\",\"realPrice\":\"240\",\"colourInventory\":\"XS:9|S:37|M:133|L:126|XL:43|XXL:23|\"}]},{\"existLink\":\"https://arcteryx.com/us/jp/shop/X000009141\",\"arcteryColourVoList\":[{\"colourLabel\":\"Stone Green\",\"realPrice\":\"240\",\"colourInventory\":\"XS:0|S:0|M:11|L:71|XL:33|XXL:16|\"},{\"colourLabel\":\"Black X\",\"realPrice\":\"240\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:193|XXL:77|\"},{\"colourLabel\":\"Canvas X\",\"realPrice\":\"240\",\"colourInventory\":\"XS:9|S:37|M:133|L:126|XL:43|XXL:23|\"}]},{\"existLink\":\"https://arcteryx.com/ca/en/shop/X000009141\",\"arcteryColourVoList\":[{\"colourLabel\":\"Stone Green\",\"realPrice\":\"280\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:147|XXL:71|\"},{\"colourLabel\":\"Canvas\",\"realPrice\":\"280\",\"colourInventory\":\"XS:3|S:0|M:110|L:135|XL:162|XXL:57|\"}]},{\"existLink\":\"https://arcteryx.com/ca/jp/shop/X000009141\",\"arcteryColourVoList\":[{\"colourLabel\":\"Stone Green\",\"realPrice\":\"280\",\"colourInventory\":\"XS:0|S:0|M:0|L:0|XL:147|XXL:71|\"},{\"colourLabel\":\"Canvas\",\"realPrice\":\"280\",\"colourInventory\":\"XS:3|S:0|M:110|L:135|XL:162|XXL:57|\"}]},{\"existLink\":\"https://arcteryx.com/it/en/shop/X000009141\",\"arcteryColourVoList\":[{\"colourLabel\":\"Stone Green\",\"realPrice\":\"240\",\"colourInventory\":\"XS:11|S:68|M:109|L:146|XL:95|XXL:27|\"},{\"colourLabel\":\"Canvas\",\"realPrice\":\"240\",\"colourInventory\":\"XS:14|S:11|M:17|L:32|XL:43|XXL:29|\"},{\"colourLabel\":\"Black\",\"realPrice\":\"240\",\"colourInventory\":\"XS:60|S:0|M:123|L:329|XL:224|XXL:76|\"}]},{\"existLink\":\"https://arcteryx.com/it/jp/shop/X000009141\",\"arcteryColourVoList\":[{\"colourLabel\":\"Stone Green\",\"realPrice\":\"240\",\"colourInventory\":\"XS:11|S:68|M:109|L:146|XL:95|XXL:27|\"},{\"colourLabel\":\"Canvas\",\"realPrice\":\"240\",\"colourInventory\":\"XS:14|S:11|M:17|L:32|XL:43|XXL:29|\"},{\"colourLabel\":\"Black\",\"realPrice\":\"240\",\"colourInventory\":\"XS:60|S:0|M:123|L:329|XL:224|XXL:76|\"}]}]}";

        R<List<ArcteryLinkVo>> listR = JSON.parseObject(json, new TypeReference<R<List<ArcteryLinkVo>>>() {
        });
        return listR;*/
    }


}
